# -*- coding: utf-8 -*-
"""
Created on Sun Nov 11 20:38:09 2018

@author: 陈忠涛
"""

import tushare as ts
import time
import pandas as pd
from dateutil.parser import parse
import matplotlib.pyplot as plt

pro = ts.pro_api('0089d005171fb2f0b539ef8e8c5c7691471c0beb94ae2ab3315a87ec')

# 数据处理函数
def getData_new(data):
    data.index = range(1,len(data) + 1)
    new_data_reslut = data.drop(['high', 'low', 'pre_close', 'change', 'pct_change', 'vol', 'amount'], axis = 1)
    new_month = []
    for m in new_data_reslut['trade_date']:
        tra_mon = time.strptime(str(parse(m)),'%Y-%m-%d %H:%M:%S').tm_mon
        new_month.append(tra_mon)
    new_data_reslut['month'] = new_month
    return new_data_reslut

data_changcheng = pro.daily(ts_code='000018.SZ', start_date='20160101', end_date='20161231')  # 神州长城
data_shuma = pro.daily(ts_code = '000034.SZ', start_date='20160101', end_date='20161231') # 神州数码

new_changcheng = getData_new(data_changcheng) # 神州长城
new_shuma = getData_new(data_shuma) # 神州数码

# plt.subplot(211)
# c_mean = new_changcheng.groupby('month')['open'].mean()
# plt.plot(c_mean.index, c_mean.values, 'red')
# plt.subplot(212)
# s_mean = new_shuma.groupby('month')['open'].mean()
# plt.plot(s_mean.index, s_mean.values, '--')

# 使用pandas绘图：柱形图

quo = pd.DataFrame()
quo['changcheng_mean'] = new_changcheng.groupby('month')['open'].mean()
quo['shuma_mean'] = new_shuma.groupby('month')['open'].mean()
quo.plot(kind = 'bar')  # 柱状图